﻿using D.Traceability.Domain;
using D.Traceability.ErrorCode;
using D.Traceability.Infrastructure.Infrastructure;
using D.Traceability.Write.API.APPlicant.Command;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.Traceability.Write.API.APPlicant.CommandHandler
{
    /// <summary>
    /// 修改溯源单个对象(通过 -- 生产批次查询)
    /// </summary>
    public class UpdateTraceCommandHandler : IRequestHandler<UpdateTraceCommand, APIResult<string>>
    {

        private readonly IBaseRepository<SU_TraceabilityModel> baseRepository;

        public UpdateTraceCommandHandler(IBaseRepository<SU_TraceabilityModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 修改溯源单个对象(通过 -- 生产批次查询)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<string>> Handle(UpdateTraceCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            //查询修改的model
            var obj = await baseRepository.GetAll().Result.FirstOrDefaultAsync(x => x.TraceaId == request.TraceaId);
            obj.GoodsName = request.GoodsName;
            obj.TraceaQualityDay = request.TraceaQualityDay;
            obj.TraceaLicenseCode = request.TraceaLicenseCode;
            obj.TraceaEnterpriseName = request.TraceaEnterpriseName;
            obj.TraceaUpdateName = request.TraceaUpdateName;
            obj.TraceaUpdateTime = DateTime.Now;
            obj.TraceaPicture = request.TraceaPicture;

            //修改
            int res = baseRepository.Update(obj).Result;
            result.Code = APIEnum.修改成功;
            result.Msg = APIEnum.修改成功.ToString();
            result.data = res.ToString();
            return await Task.FromResult(result);
        }
    }
}
